package com.water.wfo.user.controller;

import com.water.wfo.component.api.entity.response.BasicResponse;
import com.water.wfo.component.constant.NumberConstans;
import com.water.wfo.component.constant.enums.CacheKeyEnum;
import com.water.wfo.component.redis.WfoRedisService;
import com.water.wfo.component.util.AESUtil;
import com.water.wfo.component.util.MD5Util;
import com.water.wfo.component.util.StringUtil;
import com.water.wfo.sys.pojo.dto.LoginUserInfoDto;
import com.water.wfo.sys.pojo.entity.UserEntity;
import com.water.wfo.sys.pojo.request.UserQueryRequest;
import com.water.wfo.sys.pojo.request.UserRequest;
import com.water.wfo.sys.pojo.response.UserResponse;
import com.water.wfo.user.ability.UserAbility;
import com.water.wfo.user.api.UserApi;
import com.water.wfo.user.business.service.ILoginOutService;
import com.water.wfo.user.business.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * packageName com.water.wfo.user.controller
 *
 * @author junhuili
 * @version JDK 8
 * @date 2024/5/18
 * @description TODO
 */
@Slf4j
@RestController
public class UserController implements UserApi {
    @Autowired
    private UserAbility userAbility;
    @Autowired
    private ILoginOutService loginOutService;
    @Autowired
    private IUserService userService;
    @Autowired
    private WfoRedisService wfoRedisService;
    @Override
    public BasicResponse<String> queryUser(@RequestBody Map<String, String> map) {

        wfoRedisService.set(CacheKeyEnum.WFO_FICATION,"redis测试","redis");
        return new BasicResponse().buildSuccess("启动成功！");
    }

    @Override
    public BasicResponse<List<UserEntity>> queryAll(Map<String ,String> map) {
        UserEntity user = new UserEntity();
        user.setLoginName(map.get("loginName"));
        List<UserEntity>  list =userAbility.queryAll(user);
        BasicResponse<List<UserEntity>> basicResponse = new BasicResponse<>();
        return basicResponse.buildSuccess(list);
    }

    @Override
    public BasicResponse<LoginUserInfoDto> login(@RequestBody Map<String, String> map){
        BasicResponse<LoginUserInfoDto> basicResponse = new BasicResponse<>();
        if(map.isEmpty()){
            return basicResponse.buildFail("登录账号为空！");
        }
        String loginId = map.get("loginId");
        String password = map.get("password");
        LoginUserInfoDto loginUserInfoDto;
        try {
            loginUserInfoDto = loginOutService.login(loginId,password);
            return  basicResponse.buildSuccess(loginUserInfoDto);
        } catch (Exception e) {
            loginUserInfoDto = new LoginUserInfoDto();
            log.error(e.getMessage());
            return basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> loginOut(@RequestBody Map<String, String> map) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(map.isEmpty()){
            return basicResponse.buildFail("退出登录！");
        }
        String loginId = map.get("loginId");
        String password = map.get("password");
        try {
           loginOutService.loginOut();
            return  basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error(e.getMessage());
            return basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<UserResponse> queryUserListInfo(@RequestBody UserQueryRequest request) {
        BasicResponse<UserResponse> basicResponse = new BasicResponse<>();
        if(null == request){
            return basicResponse.buildFail("入参为空");
        }
        try {
            UserResponse response = userService.queryUserListInfo(request);
            return basicResponse.buildSuccess(response);
        } catch (Exception e) {
            log.error("查询客户集合异常:",e.getMessage());
            return  basicResponse.buildFail("查询客户集合异常");
        }
    }

    @Override
    public BasicResponse<String> addUserInfo(@RequestBody UserRequest userEntity) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == userEntity){
            return basicResponse.buildFail("入参为空");
        }
        try {
            userService.saveUser(userEntity);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("新增用户异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> editUserInfo(@RequestBody UserRequest userEntity) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == userEntity){
            return basicResponse.buildFail("入参为空");
        }
        try {
            userService.updateUser(userEntity);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("修改用户异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> resetPassword(@RequestParam("userId") Long userId) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == userId){
            return basicResponse.buildFail("入参为空");
        }
        try {
            UserEntity userEntity = new UserEntity();
            userEntity.setId(userId);
            userEntity.setPassword(MD5Util.getMD5Encoding("123456"));
            userAbility.update(userEntity);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("重置密码异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> deleteUser(@RequestParam("userId") Long userId) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(null == userId){
            return basicResponse.buildFail("入参为空");
        }
        try {
            userAbility.deleteByloginId(userId);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("删除用户异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }

    @Override
    public BasicResponse<String> batchDeleteUser(@RequestBody Map<String,Object> map) {
        BasicResponse<String> basicResponse = new BasicResponse<>();
        if(map.isEmpty()){
            return basicResponse.buildFail("入参为空");
        }
        try {
            List<Long> ids = (List<Long>) map.get("ids");
            userAbility.deleteBatchByIds(ids);
            return basicResponse.buildSuccess();
        } catch (Exception e) {
            log.error("删除用户异常:",e);
            return  basicResponse.buildFail(e.getMessage());
        }
    }
}
